package db

import (
	"errors"
	"github.com/spf13/cast"
	"gorm.io/gorm"
	"hr/api/dto"
)

type Post struct {
	AppModel
	Name     string `gorm:"size:64" json:"name"`
	Code     string `gorm:"size:64" json:"code"`
	Sort     string `gorm:"size:64" json:"sort"`
	CreateBy string `gorm:"size:128" json:"createBy"` // 创建人
	UpdateBy string `gorm:"size:128" json:"updateBy"` // 更新人
	Remark   string `gorm:"size:255" json:"remark"`   //备注
	Status   string `gorm:"size:64;" json:"status"`
	PostType string `gorm:"size:64;" json:"post_type"`
}

func (m *Post) TableName() string {
	return "post"
}

type PostAndTypeName struct {
	Post
	PostTypeName string `json:"post_type_name"`
}

type PostPageModel struct {
	Total    int64             `json:"total"`
	Result   []PostAndTypeName `json:"result"`
	PageNo   int               `json:"pageNo"`
	PageSize int               `json:"pageSize"`
}

func GetPostPaging(pagination dto.Pagination, name string) (PostPageModel, error) {
	result := PostPageModel{
		Total:    0,
		Result:   make([]PostAndTypeName, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	dbs := GetInstance().Select("post.*,dict_value.tag AS post_type_name").Joins("left join dict_value on dict_value.id = `post`.post_type").Model(&Post{})
	if name != "" {
		dbs = dbs.Where("post.name LIKE ?", "%"+name+"%")
	}

	if err := dbs.Offset(offset).Limit(pagination.PageSize).Find(&result.Result).Error; err != nil {
		return result, err
	}
	if err := dbs.Offset(-1).Limit(-1).Count(&result.Total).Error; err != nil {
		return result, err
	}
	return result, nil
}

// GetPosts 根据条件查找
func GetPosts(p map[string]interface{}) (Post, error, string) {
	f := Post{}
	err := GetInstance().Where(p).First(&f).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return f, errors.New("查无记录"), "0"
		}
		return f, errors.New(err.Error()), "1"
	}
	return f, nil, "2"
}

func CreatePost(p dto.PostRequest, user string) (Post, error) {
	d := Post{
		Name:     p.Name,
		Code:     p.Code,
		Sort:     cast.ToString(p.Sort),
		Status:   p.Status,
		CreateBy: user,
		Remark:   p.Remark,
		PostType: cast.ToString(p.PostType),
	}
	if err := GetInstance().Create(&d).Error; err != nil {
		return d, err
	}
	return d, nil
}

func UpdatePost(id uint, p dto.PostRequest, userId string) (Post, error) {
	f := Post{
		AppModel: AppModel{
			ID: id,
		},
	}
	err := GetInstance().Model(&f).Updates(Post{
		Name:     p.Name,
		Code:     p.Code,
		Sort:     cast.ToString(p.Sort),
		UpdateBy: userId,
		Status:   p.Status,
		Remark:   p.Remark,
		PostType: cast.ToString(p.PostType),
	}).Updates(map[string]interface{}{
		"id": id,
	}).Scan(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}

func DeletePost(id uint) (Post, error) {
	f := Post{}
	err := GetInstance().Unscoped().Where("id = ?", id).Delete(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}
